﻿package com.msjt.util;

import java.util.List;

/**
 * 查询总的记录数及结果集对象的分页模型类
 * 
 * @author kw
 * 
 */
public class PageMode {

	public PageMode() {

	}

	public PageMode(int pageSize, int pageIndex, String action,
			String modelDrive) {
		super();
		this.pageSize = pageSize;
		this.pageIndex = pageIndex;
		this.action = action;
		this.modelDrive = modelDrive;
	}

	// 记录的结果集
	private List<?> datas;

	// 每一页显示的行数
	private int pageSize;

	// 定义总行数
	private int sumRows;

	// 总页数
	private int sumPag;

	// 当前页数
	private int pageIndex;

	// 分页符
	private String pagingStr;

	// 控制的方法
	private String action;

	// 模型驱动的对象名
	private String modelDrive;

	// 初始样式
	private String initClassName;

	// 当前页样式
	private String nowClassName;

	public String getInitClassName() {
		return initClassName;
	}

	public void setInitClassName(String initClassName) {
		this.initClassName = initClassName;
	}

	public String getNowClassName() {
		return nowClassName;
	}

	public void setNowClassName(String nowClassName) {
		this.nowClassName = nowClassName;
	}

	public String getModelDrive() {
		return modelDrive;
	}

	public void setModelDrive(String modelDrive) {
		this.modelDrive = modelDrive;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getPagingStr() {
		return pagingStr;
	}

	public void setPagingStr(String pagingStr) {
		this.pagingStr = pagingStr;
	}



	public int getSumRows() {
		return sumRows;
	}

	public void setSumRows(int sumRows) {
		this.sumRows = sumRows;

		// 创建分页符
		//this.createPageStr();
	}

	public int getSumPag() {
		return sumPag;
	}

	public void setSumPag(int sumPag) {
		this.sumPag = sumPag;
	}


	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public List<?> getDatas() {
		return datas;
	}

	public void setDatas(List<?> datas) {
		this.datas = datas;
	}

	// 创建分页提示符
	public void createPageStr() {
		 String style="<style type=\"text/css\">" +
		 		"DIV.digg {PADDING-RIGHT: 3px; PADDING-LEFT: 3px; " +
		 		"PADDING-BOTTOM: 3px; MARGIN: 3px; PADDING-TOP: 3px; TEXT-ALIGN: center}" +
		 		"DIV.digg A " +
		 		"{BORDER-RIGHT: #aaaadd 1px solid;" +
		 		" PADDING-RIGHT: 5px; " +
		 		"BORDER-TOP: #aaaadd 1px solid; " +
		 		"PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #aaaadd 1px solid; COLOR: #000099; PADDING-TOP: 2px; " +
		 		"BORDER-BOTTOM: #aaaadd 1px solid; TEXT-DECORATION: none}" +
		 		"DIV.digg A:hover {" +
		 		"BORDER-RIGHT: #000099 1px solid; BORDER-TOP: #000099 1px solid; BORDER-LEFT: #000099 1px solid;" +
		 		" COLOR: #000; BORDER-BOTTOM: #000099 1px solid" +
		 		"}" +
		 		"DIV.digg A:active {" +
		 		"BORDER-RIGHT: #000099 1px solid; BORDER-TOP: #000099 1px solid; BORDER-LEFT: #000099 1px solid; " +
		 		"COLOR: #000; BORDER-BOTTOM: #000099 1px solid" +
		 		"}" +
		 		"DIV.digg SPAN.current {" +
		 		"BORDER-RIGHT: #000099 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #000099 1px solid; " +
		 		"PADDING-LEFT: 5px; FONT-WEIGHT: bold; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #000099 1px solid; " +
		 		"COLOR: #fff; PADDING-TOP: 2px; BORDER-BOTTOM: #000099 1px solid; BACKGROUND-COLOR: #000099" +
		 		"}" +
		 		"DIV.digg SPAN.disabled {" +
		 		"BORDER-RIGHT: #eee 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #eee 1px solid; " +
		 		"PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #eee 1px solid; COLOR: #ddd; " +
		 		"PADDING-TOP: 2px; BORDER-BOTTOM: #eee 1px solid" +
		 		"}</style>";
		// 通过总行数和传递过来的每页显示的行数来得到总页数sum_pagination
		if (this.getSumRows() % this.getPageSize() == 0) {
			this.setSumPag(this.sumRows / this.pageSize);
		} else {
			this.setSumPag(this.sumRows / this.pageSize + 1);
		}
		//标头提示字符串换
		StringBuffer showNow=new StringBuffer();
		//首页提示符
		StringBuffer first=new StringBuffer();
		//最后一页
		StringBuffer last=new StringBuffer();
		// 页数提示符
		StringBuffer buffer = new StringBuffer();
		showNow.append("<font color=\"green\">总页数</font>   [<font color=\"red\">");
		showNow.append(this.getSumPag());
		showNow.append("</font>]   ");
		showNow.append("<font color=\"green\">当前页</font>   [<font color=\"red\">"+ this.getPageIndex());
		showNow.append("</font>]");
		//当前页是第一页的时候,首页与最后一页的样式
		first.append("<a href='");
		first.append(this.getAction());
		first.append("?");
		first.append(this.getModelDrive());
		first.append(".pageIndex=1");
		first.append("&");
		first.append(this.getModelDrive());
		first.append(".pageSize=");
		first.append(this.getPageSize());
		first.append("'>");
		first.append(" 首页 ");
		first.append("</a>");
		//最后一页
		last.append("<a href='");
		last.append(this.getAction());
		last.append("?");
		last.append(this.getModelDrive());
		last.append(".pageIndex=");
		if(this.sumPag==0){
			last.append(1);
		}else{
			last.append(this.getSumPag());
		}
		last.append("&");
		last.append(this.getModelDrive());
		last.append(".pageSize=");
		last.append(this.getPageSize());
		last.append("'>");
		last.append("末页 ");
		last.append("</a>");
		// 得到分页提示信息
		//for (int i = 1; i <= this.getSumPag(); i++) {
		//拼接当前页的前后3页的链接
		int k=this.pageIndex+5;
		if(this.sumPag==0){
			k=1;
		}
		for (int i = this.pageIndex-5; i <= k; i++) {
			if(i<=0){
				continue;
			}
			//如果页面将要大出总页数，则后面不在拼接超链接
			if(i==this.getSumPag()){
				k=this.getSumPag();
			}
			//设置当前页的格式
			if (i == this.getPageIndex()) {
				buffer.append("<span class=\"current\">");
				buffer.append(i);
				buffer.append("</span>");
				continue;
			}
			buffer.append("<a class='");
			buffer.append("' href='");
			buffer.append(this.getAction());
			buffer.append("?");
			buffer.append(this.getModelDrive());
			buffer.append(".pageIndex=");
			buffer.append(i);
			buffer.append("&");
			buffer.append(this.getModelDrive());
			buffer.append(".pageSize=");
			buffer.append(this.getPageSize());
			buffer.append("'>");
			buffer.append(i);
			buffer.append("</a>");
		}
		StringBuffer total=new StringBuffer();
		total.append(style);
		total.append("<div class=\"digg\">");
		//total.append(showNow);
		total.append(first);
		total.append(buffer);
		total.append(last);
		total.append("</div>");
		this.setPagingStr(total.toString());
	}
}
